package com.cloudptt.api.product.adapter;

import java.util.List;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.comm.Const;
import com.cloudptt.api.models.CallDescInfo;
import com.cloudptt.api.pjsip.service.AppRTCAudioManager;
import com.cloudptt.api.pjsua2.MyCall;
import com.cloudptt.api.product.ILoginChangeListener;
import com.cloudptt.api.product.ISetDefaultGroup;
import com.cloudptt.api.product.ISuperCallOperator;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.UpdateUiListener;
import com.cloudptt.api.product.device.DeviceInfo;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallListener;
import com.cloudptt.api.product.func.call.group.HalfCallInfo;
import com.cloudptt.api.product.func.call.group.GroupCallSpeakListener;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.func.media.IEventAdapter;
import com.cloudptt.api.product.func.media.light.LightManager;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.api.product.operator.CallOperator;
import com.cloudptt.api.product.operator.SysMsgOperator;
import com.cloudptt.api.product.res.Resource;
import com.cloudptt.api.product.ringer.PromptTone;
import com.mcptt.config.LoginInfoStruct;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.UserInfo;

import android.content.Context;
import android.view.KeyEvent;

public class GCallEventAdapter implements IEventAdapter,CallListener,GroupCallSpeakListener,IRollback, ISetDefaultGroup
{

	private static String THIS_FILE = "GCallEventAdapter";
	private Context ctx = null;

	protected Product product = null;
	protected ISuperCallOperator callOperator = null;
	//	protected CallOperator privateOperator = null;
	private LightManager lightManager = null;
//	private int batterValue = 100;

	//	private MenuAdapter menuAdapter = null;
//	private SmsCenter smsCenter = null;
	private String curCallKey = null;
	private ICallInfoDesc desc = null;
	private CallDescInfo callDescInfo = null;

	private CallListener callListener = null;
	private GroupCallSpeakListener floorListener = null;

	private ILoginChangeListener mListener;
	private boolean isGroup = true;
	private String lastStateDesp = "";
	private String lastFloorStr = "";

	private boolean isRecvSos = false;
	private boolean isExitSos = true;
	private UpdateUiListener updateUiListener = null;
	private boolean isRecvTaken = false;
	private int floorState = CallConst.FloorState_Null;

	private ISingleHalfCallNotify singleHalfCallNotify = null;


	public void setILoginChangeListener(ILoginChangeListener listener){
		mListener = listener;
	}

	public void setCallInfoDesc(ICallInfoDesc desc) {
		Log.i(THIS_FILE, "setGCallDescTv desc = " + desc + " curCallKey = " + curCallKey);
		if(this.desc != desc) {
			this.desc = desc;
			if (curCallKey != null
					&& callOperator != null) {
				HalfCallInfo callInfo = callOperator.getHalfCallInfo(curCallKey);
				updateCallDescByCallInfo(curCallKey, callInfo);
			}
		}
	}

	public void setHalfCallListener(CallListener callListener,GroupCallSpeakListener floorListener)
	{
		this.callListener = callListener;
		this.floorListener = floorListener;
	}


	public void setCurCallKey(String callKey) {
//		Log.i(THIS_FILE, "setCurCallKey callKey = " + callKey);
//		this.curCallKey = callKey;
//		this.curCallKey = GroupInfo.Group_Num_All;
	}


	public GCallEventAdapter(Context ctx,Product product,int iconId,Class cls)
	{
		this.ctx = ctx;
		if(DeviceInfo.isPNC370()
			|| DeviceInfo.isPNC380()) {
			this.lightManager = new LightManager(ctx, iconId, cls);
		}
		this.product = product;
		this.product.setDefaultGroupListener(this);
//		this.product.setGroupCallListener(this, this);
//		this.batterValue = product.getBatteryValue();
		this.callOperator = product.getSuperCallOperator();
		this.callDescInfo = new CallDescInfo();
	}

	public void doNetworkChange(boolean isConn)
	{

	}

	public void onLogin(boolean isLogin)
	{
		if(lightManager != null)
		{
			lightManager.onLogin(isLogin);
		}
	}

	public void clearResource()
	{
		if(product != null)
		{
			product.setDefaultGroupListener(null);
		}
		if(lightManager != null)
		{
			lightManager.clearResouce();
			lightManager = null;
		}

		if(mListener != null){
			mListener = null;
		}
	}

	@Override
	public void onPtt(int type,boolean isDefaultGroup,boolean isRing) {
		// TODO Auto-generated method stub
//		closeMenu();
		pttBtnAction(type,isDefaultGroup,isRing);
	}

	@Override
	public void onFreq(int index) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onFreq:" + "index:" + index);
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onLogin ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onFreq product is null");
		}

		if(ctx == null)
		{
			return;
		}

		AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
		final List<GroupInfo> grouplist = operator.getGroupList();

		Log.d(THIS_FILE, "onFreqAction channel: = " + index);
		if(index < 0
				|| index >= grouplist.size())
		{
			return;
		}

		GroupInfo info = null;
		if (index <= grouplist.size()
				&& grouplist.size() > 0
				&& index > 0)
		{
			info = grouplist.get(index);
		}
		AddrInfo curInfo = product.getCurGroupInfo();
		int result = operator.checkOperate();
		if(CauseCont.OperateCheck_Suc != result)
		{
			operator.startSoundPlayer(PromptTone.Type_Disconnect);
		}

		if(curInfo != null
				&& !curInfo.isSameOne(info))
		{
			hangUpGroupCall();
		}

		product.setDefaultGroupInfo(info);
//		soundGroupName();
	}

	@Override
	public void onUpDownFreq(int type) {
		Log.e(THIS_FILE, "onUpDownFreq  type:" + type);


		if(product == null)
		{
			return;
		}
		AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
		final List<GroupInfo> grouplist = operator.getGroupList();

		int targetindex = 0;
		AddrInfo curInfo = product.getCurGroupInfo();

		if(curInfo != null
				&& grouplist != null)
		{
			for(int i=0;i<grouplist.size();i++)
			{
				if(curInfo.isSameOne(grouplist.get(i)))
				{
					targetindex = i;
				}
			}
		}
		Log.d(THIS_FILE, "eventAdapter.onUpDownFreq(Const.FREQ_UP);" + targetindex);

		if(type==Const.FREQ_UP)
		{

			if(targetindex==0)
			{
				targetindex= grouplist.size()-1;
			}
			else
			{
				targetindex= targetindex-1;
			}
		}
		else if(type==Const.FREQ_DOWN)
		{
			if(targetindex==grouplist.size()-1)
			{
				targetindex= 0;
			}
			else
			{
				targetindex= targetindex+1;
			}
		}
		else
		{
			return;
		}

		Log.d(THIS_FILE, "onUpDownFreq 2 targetindex:" + targetindex);

		if(targetindex >=0
				&& targetindex <grouplist.size())
		{
			GroupInfo info = grouplist.get(targetindex);

			//			AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
			int result = operator.checkOperate();
			if(CauseCont.OperateCheck_Suc != result)
			{
				operator.startSoundPlayer(PromptTone.Type_Disconnect);
			}
			if(curInfo != null
					&& !curInfo.isSameOne(info))
			{
				hangUpGroupCall();
			}

			product.setDefaultGroupInfo(info);
			product.setCurGroupInfo(null);
//			soundGroupName();
		}

	}



	@Override
	public void onUserDefined1(boolean isLongPress) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onUserDefined1:" + "isLongPress:" + isLongPress );
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onUserDefined1 ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onUserDefined1 product is null");
		}
		if(ctx == null)
		{
			return;
		}

//		if(isLongPress)
//		{
//			if(lightManager != null)
//			{
//				lightManager.openLED(false, LightManager.LED_BLUE);
//			}
//		}
//		else
//		{
		onShortUserDefined1Action();
//		}
	}



	@Override
	public void onUserDefined3(boolean isLongPress) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onUserDefined3:" + "isLongPress:" + isLongPress);
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onUserDefined3 ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onUserDefined3 product is null");
		}
		if(ctx == null)
		{
			return;
		}

		if(!isLongPress)
		{
			onShortUserDefined3Action(curCallKey);
		}
//		else
//		{
//			onLongUserDefined3Action(curCallKey);
//		}
	}


	@Override
	public void onSOS(boolean isLongPress) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onSOS:" + "isLongPress:" + isLongPress);
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onSOS ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onSOS product is null");
		}
		if(ctx == null)
		{
			return;
		}
		if(callOperator == null)
		{
			this.callOperator = product.getSuperCallOperator();
		}
		Log.d(THIS_FILE, "onSOS isAlarm is " + product.getSysConfig().isAlarm());
		if(!product.getSysConfig().isAlarm())
		{
			return;
		}
		CallOperator operator = callOperator.getCallOperator();
		GroupInfo info = product.getSysConfig().getSosGroupInfo();
		Log.d(THIS_FILE, "onSOS info = " + info + " isRecvSos = " + isRecvSos);
		if(this.isRecvSos) {
			operator.ringOrStopSos();
			operator.ringStop();
		}
		else
		{
			AddrInfo curDefaultGroupInfo = product.getCurGroupInfo();
			Log.d(THIS_FILE, "onSOS curDefaultGroupInfo = " + curDefaultGroupInfo);
			if(curDefaultGroupInfo != null
					&& !curDefaultGroupInfo.isSameOne(info))
			{
				if(info != null) {
					SysMsgOperator op = (SysMsgOperator) product.getOperator(ProductConst.Func_SysMsg);
					if (op != null) {
						op.sendSosMsg();
					}
					product.setOldDefaultGroupInfo(curDefaultGroupInfo);
					product.setDefaultGroupInfo(info);
					product.setCurGroupInfo(null);
					operator.ringShortSos();
				}
			}
		}
		if(isLongPress)
		{
			AddrInfo oldDefaultGroupInfo = product.getOldDefaultGroupInfo();
			Log.d(THIS_FILE, "onSOS oldDefaultGroupInfo = " + oldDefaultGroupInfo);
			if(oldDefaultGroupInfo != null) {
				product.setDefaultGroupInfo(oldDefaultGroupInfo);
				product.setCurGroupInfo(null);
				operator.ringStop();
			}
			isExitSos = true;
		}

		this.isRecvSos = false;

		if(this.updateUiListener != null
				&& info != null)
		{
			Log.d(THIS_FILE, "onSOS isExitSos = " + isExitSos);
			int type = UpdateUiListener.Type_SosStr;
			if(!isExitSos)
			{
				type = UpdateUiListener.Type_Short;
			}
			if(isLongPress)
			{
				type = UpdateUiListener.Type_State;
			}
			updateUiListener.UpdateUi(type,"");
		}
	}

	private void onShortUserDefined1Action()
	{
//		if (privateOperator != null)
//		{
//			boolean isActiveCall = privateOperator.isActiveCall();
//			boolean isIncommingCall = privateOperator.isIncommingCall();
//			Log.d(THIS_FILE, "onPrivateCallAction isActiveCall = "
//					+ isActiveCall + " isIncommingCall = "
//					+ isIncommingCall);
//			if (isIncommingCall) {
//				//				int result = privateOperator
//				//						.answerCall(SipCallSession.Call_Privacy);
//				//				if (result != CauseCont.OperateCheck_Suc) {
//				//					Log.d(THIS_FILE,
//				//							" ------ onAcceptPrivateCall failed    ");
//				//					return;
//				//				}
//			}
//			else
//			{
//				soundGroupName();
//			}
//		}
//		else
		{
//			soundGroupName();
		}
	}

//	private void soundGroupName()
//	{
//		Log.d(THIS_FILE,"soundGroupName");
//		AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
//		boolean result = operator.checkOperate();
//		if(!result)
//		{
//			operator.startSoundPlayer(PromptTone.Type_Disconnect);
//			return;
//		}
//
//		GroupInfo info = product.getCurGroupInfo();
//		if(info != null)
//		{
//			String str = ctx.getString(R.string.cur_group);
//			String groupName = info.getName();
//			if(groupName != null
//					&& groupName.length() > 0)
//			{
//				//				product.playVoiceMsg(str + groupName);
//				CommDlg.showShortToast(ctx, str + groupName);
//			}
//		}
//		else
//		{
//			String str = this.ctx.getString(R.string.null_group);
//			//			product.playVoiceMsg(str);
//			CommDlg.showShortToast(ctx, str);
//		}
//
//	}

	private void onShortUserDefined3Action(String curCallKey)
	{
		boolean isActiveCall = false;
		if (callOperator != null)
		{
			isActiveCall = callOperator.isActiveCall(curCallKey);
			if(isActiveCall)
			{
//				String str = ctx.getString(R.string.str_hangup);
//				//				product.playVoiceMsg(str);
//				CommDlg.showShortToast(ctx, str);
				callOperator.doUserAction(curCallKey,CallConst.BtnAction_Hangup);
			}
		}

//		if (!isActiveCall
//				& privateOperator != null)
//		{
//			isActiveCall = privateOperator.isActiveCall();
//			if(isActiveCall)
//			{
//				String str = ctx.getString(R.string.str_hangup);
//				//				product.playVoiceMsg(str);
//				CommDlg.showShortToast(ctx, str);
////				privateOperator.hangupCall(SipCallSession.Call_Privacy);
//			}
//		}
//		if(!isActiveCall)
//		{
//			batterValue = product.getBatteryValue();
//			onLedByBatteryChg(batterValue);
//			ringNormalWarring(batterValue);
//			//			if(batterValue < 15)
//			//			{
//			//				ringLowWarring();
//			//			}
//			//			else
//			//			{
//			//				ringNormalWarring(batterValue);
//			//			}
//		}
	}



//	private void onLongUserDefined3Action(String curCallKey)
//	{
//		McIdInfo info = product.getMcpttLoginInfo().getMcIdInfo();
//		if(info == null)
//		{
//			return;
//		}
//		String value = info.getName();
//
//		String str = ctx.getString(R.string.menu_iam);
//		StringBuffer strBuf = new StringBuffer(str);
//		strBuf.append(value);
//		str = ctx.getString(R.string.menu_num);
//		value = info.getMcId();
//		strBuf.append(str).append(value);
//
//		//		product.playVoiceMsg(strBuf.toString());
//		CommDlg.showShortToast(ctx, strBuf.toString());
//	}

	public void hangUpGroupCall()
	{
		boolean isActiveCall = callOperator.isActiveCall(curCallKey);
		if(isActiveCall)
		{
			callOperator.doUserAction(curCallKey,CallConst.BtnAction_Hangup);
		}
	}
//	public void disconnectWarring(boolean isconnected)
//	{
//		Log.d(THIS_FILE,"disconnectWarring");
//		if (!isconnected)
//		{
//			if(lightManager != null)
//			{
//				lightManager.openLED(false,LightManager.LED_RED,LightManager.Level_High);
//			}
//			GroupCallAgent callAgent = (GroupCallAgent)callOperator;
//			CallOperator operator = callAgent.getOperator( );
//			operator.startSoundPlayer(PromptTone.Type_Disconnect);
//		}
//		else
//		{
//			if(lightManager != null)
//			{
//				lightManager.closeLED(LightManager.Level_High);
//			}
//		}
//	}
//
//	public void loginWarring(boolean isLogined)
//	{
//		Log.d(THIS_FILE,"loginWarring isLogined = " + isLogined);
//		if(lightManager == null)
//		{
//			return;
//		}
//		lightManager.onLogin(isLogined);
//		if (!isLogined)
//		{
//			lightManager.openLED(false,LightManager.LED_RED,LightManager.Level_High);
//		}
//		else
//		{
//			lightManager.closeLED(LightManager.Level_High);
//		}
//	}
//
//	public void soundLinking()
//	{
//		GroupCallAgent callAgent = (GroupCallAgent)callOperator;
//		CallOperator operator = callAgent.getOperator( );
//		operator.startSoundPlayer(PromptTone.Type_Linking);
//	}

	public void openOperateLED(boolean isFlash, int ledColor)
	{
		if(lightManager != null)
		{
//			lightManager.resetLED();
			lightManager.openLED(isFlash,ledColor,LightManager.Level_Low);
		}
	}
	public void closeOperateLED()
	{
		if(lightManager != null) {
			lightManager.closeLED(LightManager.Level_Low);
		}

	}

//	public void ringLowWarring()
//	{
//		GroupCallAgent callAgent = (GroupCallAgent)groupCallOperator;
//		CallOperator operator = callAgent.getOperator( );
//		operator.startSoundPlayer(PromptTone.Type_Low);
//	}
//	private void ringNormalWarring(int value)
//	{
//		GroupCallAgent callAgent = (GroupCallAgent)groupCallOperator;
//		CallOperator operator = callAgent.getOperator( );
//		boolean result = operator.checkOperate();
//		if(!result)
//		{
//			operator.startSoundPlayer(PromptTone.Type_Normal);
//		}
//		else
//		{
//			String str = ctx.getString(R.string.battery);
//			str = str + value + "%";
//			//			product.playVoiceMsg(str);
//			CommDlg.showShortToast(ctx, str);
//		}
//	}

	private void pttBtnAction(int type,boolean isDefaultGroup,boolean isRing)
	{
		Log.d(THIS_FILE,"pttBtnAction type = " + type + " isDefaultGroup = " + isDefaultGroup + " curCallKey = " + curCallKey);
		if(ctx == null)
		{
			return;
		}

//		ISuperCallOperator callTracker = (ISuperCallOperator)callOperator;
//		CallOperator operator = callTracker.getOperator( );
//		boolean result = operator.checkOperate();
		AddrInfo info = product.getCurGroupInfo();
//		Log.d(THIS_FILE,"pttBtnAction result = " + result);
		Log.d(THIS_FILE,"pttBtnAction info = " + info);
		if(Const.MSG_TALK_ON == type) {
			if(product == null
					|| !product.checkNetWork())
			{
				String str = Resource.getString("network_offline");//ctx.getString(R.string.null_group);
				CommDlg.showShortToast(ctx, str);
			}
			if (info == null
					|| info.isSameOne(GroupInfo.getDefaultUserInfo())) {
				String str = Resource.getString("null_group");//ctx.getString(R.string.null_group);
				CommDlg.showShortToast(ctx, str);
				return;
			}
		}

		ISuperCallOperator  operator = product.getSuperCallOperator();
//		if(operator.isHaveActiveWebrtcCall())
//		{
//			if(Const.MSG_TALK_ON == type)
//			{
//				String str = Resource.getString("have_active_pcall");//ctx.getString(R.string.null_group);
//				CommDlg.showShortToast(ctx, str);
//			}
//			return;
//		}
		if(curCallKey == null)
		{
			curCallKey = product.getCurGroupCallKey();
		}

		boolean canDown = false;
		boolean isActiveCall = false;
		HalfCallInfo callInfo = operator.getHalfCallInfo(curCallKey);
		Log.d(THIS_FILE,"pttBtnAction callInfo = " + callInfo + " curCallKey = " + curCallKey);
		if(callInfo == null)
		{
			canDown = true;
			isActiveCall = false;
		}
		else
		{
			int callState = callInfo.getTalkState();
			int SpeakState = callInfo.getSpeakState();
			int videoState = callInfo.getVideoState();
			Log.d(THIS_FILE,"pttBtnAction callState = " + callState + " SpeakState = " + SpeakState);
			canDown = canPttBtnDown(callState,SpeakState,videoState);
			isActiveCall = isActiveCall(callState);
		}

		if (type == Const.MSG_TALK_ON
				&& canDown)
		{
			int isSuc = CauseCont.OperateErr_Exception;
//			if(!isActiveCall)
//			{
//				makeGroupCall();
//			}
//			else
			{
				isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Request);
			}
			if(CauseCont.OperateCheck_Suc == isSuc)
			{
//				callOperator.startSoundPlayer(PromptTone.Type_On);
			}
			else if(CauseCont.OperateErr_NoAgent != isSuc)
			{
				String errStr = CauseCont.getOperateCheckErrStr(isSuc, ctx);
				CommDlg.showToast(ctx, errStr);
//				callOperator.startSoundPlayer(PromptTone.Type_Err);
			}

		} else if (type == Const.MSG_TALK_OFF)
		{
//			int isSuc = CauseCont.OperateErr_Exception;
//			if(!isActiveCall)
//			{
//				isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Hangup);
//			}
//			else
//			{
//				isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Release);
//			}
			HalfCallInfo gCallInfo = callOperator.getHalfCallInfo(curCallKey);
			Log.d(THIS_FILE,"pttBtnAction gCallInfo = " + gCallInfo);
//			if(gCallInfo != null
//					&& gCallInfo.getSpeakState() != CallConst.FloorState_Permission)
//			{
//				return;
//			}
			int isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Release);
			if(CauseCont.OperateCheck_Suc == isSuc)
			{
				if(isRing) {
					callOperator.startSoundPlayer(PromptTone.Type_Off);
				}
			}
			else if(CauseCont.OperateErr_OpQuick == isSuc)
			{
				String str = Resource.getString("operator_quick");
				CommDlg.showShortToast(ctx, str);
			}
			else if(CauseCont.OperateErr_NoAgent != isSuc)
			{
				String errStr = CauseCont.getOperateCheckErrStr(isSuc, ctx);
				CommDlg.showToast(ctx, errStr);
			}
			closeOperateLED();
		}
	}

	public void makeGroupCall()
	{
		Log.d(THIS_FILE,"makeGroupCall");
		boolean hasCall = callOperator.isHasCall(curCallKey);
		Log.d(THIS_FILE,"makeGroupCall hasCall = " + hasCall);
		if(hasCall)
		{
			return;
		}
		AddrInfo groupInfo = product.getSysGroupInfo();
		Log.d(THIS_FILE,"makeGroupCall groupInfo = " + groupInfo);
		UserInfo loginUserInfo = product.getMcpttLoginInfo().getMcIdInfo().toUserInfo();
		List<String> idList = product.getGroupIDList();
		MyCall.setIdList(idList);
		CallAttr callAttr = new CallAttr(groupInfo,loginUserInfo,CallAttr.CallDir_MO,isGroup);
		int isSuc = callOperator.newCall(callAttr);
		Log.d(THIS_FILE,"makeGroupCall isSuc = " + isSuc);
		if(CauseCont.OperateCheck_Suc == isSuc)
		{
//					if(isDefaultGroup)
			{
//				this.curCallKey = callAttr.getCallKey();
				this.curCallKey = GroupInfo.Group_Num_All;
				Log.d(THIS_FILE,"pttBtnAction curCallKey = " + curCallKey);
			}
			isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Make);
		}
	}

	public void doReleaseFloor()
	{
		int isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Release);
		if(CauseCont.OperateCheck_Suc == isSuc)
		{
			callOperator.startSoundPlayer(PromptTone.Type_Off);
		}
		else if(CauseCont.OperateErr_OpQuick == isSuc)
		{
			String str = Resource.getString("operator_quick");
			CommDlg.showShortToast(ctx, str);
		}
		else if(CauseCont.OperateErr_NoAgent != isSuc)
		{
			String errStr = CauseCont.getOperateCheckErrStr(isSuc, ctx);
			CommDlg.showToast(ctx, errStr);
		}
	}

	public void soundForTip()
	{
		Log.d(THIS_FILE, "soundForTip");
//		CallAgent callAgent = (CallAgent)groupCallOperator;
//		CallOperator operator = callAgent.getOperator( );
	}

	private boolean canPttBtnDown(int callState,int SpeakState,int videoState)
	{
		if(CallConst.CallState_InitState == callState
				|| CallConst.FloorState_NoPermission == SpeakState
				|| CallConst.VideoState_NoPermission == videoState)
		{
			return true;
		}
		return false;
	}

	private boolean isActiveCall(int callState)
	{
		if(CallConst.CallState_Activite == callState)
		{
			return true;
		}
		return false;
	}


	@Override
	public void updateCallState(String callKey, int callState,boolean isHasVideo) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"updateCallState callKey = " + callKey + " callState = " + callState);
//		this.curCallKey = callKey;
//		this.curCallKey = GroupInfo.Group_Num_All;
		if(callListener != null)
		{
			callListener.updateCallState(callKey, callState,isHasVideo);
		}
		if(desc != null)
		{
			if(callState == CallConst.CallState_InitState)
			{
				desc.updateCallUser(callKey, false,"");
			}
			else
			{
				AddrInfo curInfo = product.getCurGroupInfo();
				Log.d(THIS_FILE,"updateCallState callKey = " + callKey + " curInfo = " + curInfo);
				if(curInfo != null)
				{
					desc.updateCallUser(callKey, true,curInfo.getName());
				}

				if(callState == CallConst.CallState_ConnectingState
						|| callState == CallConst.CallState_DisconnectingState)
				{
					lastStateDesp = callDescInfo.getErrDesc(CallConst.FloorState_NoPermission);
					desc.updateCallInfoDesc(callKey,curInfo.getDscp(), lastStateDesp);
				}
//				else if(callState == CallConst.CallState_Activite)
//				{
//					String desp = callDescInfo.getErrDesc(CallConst.FloorState_NoPermission);
//					if(!lastStateDesp.endsWith(desp)) {
//						lastStateDesp = desp;
//						desc.updateCallInfoDesc(callKey, curInfo.getDscp(), lastStateDesp);
//					}
//				}
			}
		}
	}

	@Override
	public void updateSpeakState(String callKey, int speakState) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"lightControl updateSpeakState callKey = " + callKey + " talkState = " + speakState);
//		this.curCallKey = callKey;
		this.floorState = speakState;
		lastFloorStr = callDescInfo.getErrDesc(speakState);
		if(speakState == CallConst.FloorState_Permission)
		{
			callOperator.startSoundPlayer(PromptTone.Type_On);
		}


		if(desc != null)
		{
			String value = callDescInfo.getErrDesc(speakState);
			if(speakState == CallConst.FloorState_Null
					|| speakState == CallConst.FloorState_NoPermission
					|| speakState == CallConst.FloorState_PendingRequset
					|| speakState == CallConst.FloorState_Permission
					|| speakState == CallConst.SpeakState_PendingRelease)
			{
				AddrInfo curInfo = product.getCurGroupInfo();
				if(curInfo != null)
				{
					desc.updateCallInfoDesc(callKey, curInfo.getDscp(),value);
				}

			}
		}
		else
		{
			Log.d(THIS_FILE,"lightControl updateSpeakState desc = null");
		}
//		if(speakState == CallConst.FloorState_Permission)
//		{
//			Log.d(THIS_FILE,"lightControl send broadcast");
//			ctx.sendBroadcast(new Intent("com.cloudptt.wp.show"));
//		}
		if(floorListener != null)
		{
			floorListener.updateSpeakState(callKey, speakState);
		}
		if(speakState == CallConst.FloorState_Permission)
		{
			openOperateLED(false,LightManager.LED_RED);
		}
		else
		{
			closeOperateLED();
		}
	}

	@Override
	public void updateSpeakPermissionInfo(String callKey, AddrInfo gInfo,AddrInfo info) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"lightControl updateSpeakPermissionInfo callKey = " + callKey + " gInfo = " + gInfo + " info = " + info);
//		this.curCallKey = callKey;
		if(desc != null)
		{
			if(gInfo != null) {
				if(gInfo.isUserType()
					&& singleHalfCallNotify != null)
				{
					singleHalfCallNotify.showSingleHalfCallDlg(gInfo);
				}
				if(info != null) {//idle时不设默认组
					product.setCurGroupInfo(gInfo);
				}
				if (info != null) {
					if(!isRecvTaken)
					{
						callOperator.startSoundPlayer(PromptTone.Type_On);
					}
					isRecvTaken = true;

					String takenStr = callDescInfo.getTakenKey();
					lastFloorStr = info.getName();
					desc.updateCallInfoDesc(callKey, gInfo.getDscp(), info.getName() + takenStr);
				} else {
//					product.setCurGroupInfo(null);
					if(floorState != CallConst.FloorState_Permission) {
						String value = callDescInfo.getErrDesc(floorState);
						desc.updateCallInfoDesc(callKey, gInfo.getDscp(), value);
					}
					if(isRecvTaken)
					{
						callOperator.startSoundPlayer(PromptTone.Type_Off);
						isRecvTaken = false;
					}
				}
				Log.d(THIS_FILE,"updateSpeakPermissionInfo isRecvTaken = " + isRecvTaken);

			}
			else
			{
				Log.d(THIS_FILE,"updateSpeakPermissionInfo else isRecvTaken = " + isRecvTaken);

				AddrInfo curInfo = product.getCurGroupInfo();
				if(curInfo != null) {
//					product.setCurGroupInfo(curInfo);
					if (info != null) {
						isRecvTaken = true;
						callOperator.startSoundPlayer(PromptTone.Type_On);
						String takenStr = callDescInfo.getTakenKey();
						desc.updateCallInfoDesc(callKey, curInfo.getDscp(), info.getName() + takenStr);
					} else {
//						product.setCurGroupInfo(null);
						String value = callDescInfo.getErrDesc(CallConst.FloorState_NoPermission);
						desc.updateCallInfoDesc(callKey, curInfo.getDscp(), value);
						if(isRecvTaken)
						{
							callOperator.startSoundPlayer(PromptTone.Type_Off);
							isRecvTaken = false;
						}
					}
				}
			}
		}
		else
		{
			Log.d(THIS_FILE,"lightControl updateSpeakPermissionInfo desc = null");
		}
		if(floorListener != null)
		{
			floorListener.updateSpeakPermissionInfo(callKey, gInfo,info);
		}
		if(info != null)
		{
//			ctx.sendBroadcast(new Intent("com.cloudptt.wp.show"));
			openOperateLED(false,LightManager.LED_GREE);
		}
		else
		{
			if(floorState != CallConst.FloorState_Permission) {
				closeOperateLED();
			}
		}

	}
	public void updateSpeakDuration(String callKey,String secStr)
	{
		Log.d(THIS_FILE,"updateSpeakDuration callKey = " + callKey + " secStr = " + secStr);
		if(desc != null)
		{
			desc.updateSecDesc(callKey, secStr);
		}
		else
		{
			Log.d(THIS_FILE,"updateSpeakDuration desc = null");
		}
	}

	@Override
	public void notifyErrStr(String callKey, String errStr) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"updateSpeakState callKey = " + callKey + " errStr = " + errStr);
	}


	@Override
	public void updateMicMute(String callKey,final boolean isMicMute)
	{

	}

	@Override
	public void updateCallDuration(String callKey, String secStr) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"updateSpeakState callKey = " + callKey + " secStr = " + secStr);
//		this.curCallKey = callKey;
		if(callListener != null)
		{
			callListener.updateCallDuration(callKey, secStr);
		}
		if(desc != null)
		{
			desc.updateSecDesc(callKey, secStr);
		}
		else
		{
			Log.d(THIS_FILE,"updateCallDuration desc = null");
		}
	}

	private void updateCallDescByCallInfo(String callKey,HalfCallInfo callInfo)
	{
		Log.i(THIS_FILE, "updateCallDescByCallInfo callKey= " + callKey + " callInfo = " + callInfo);
		if(callInfo != null)
		{
			int callState = callInfo.getTalkState();
			this.updateCallState(callKey, callState,false);
//			int speakState = callInfo.getSpeakState();
//			this.updateSpeakState(callKey, speakState);
			AddrInfo gInfo = product.getCurGroupInfo();
			AddrInfo info = callInfo.getFloorUserInfo();
			this.updateSpeakPermissionInfo(callKey, gInfo,info);
		}
		else
		{
			this.updateCallState(callKey, CallConst.CallState_InitState,false);
		}
	}
	@Override
	public void networkChange(boolean isConnect) {
		Log.i(THIS_FILE, "networkChange isConnect="+isConnect);
		if(product !=null){
			product.networkChange(isConnect);
		}
	}

	@Override
	public void onReqLogin(LoginInfoStruct data) {
		Log.i(THIS_FILE, "onReqLogin");
		if(mListener!= null){
			mListener.requstLogin(data);
		}
	}

	@Override
	public void onReqConnectWatch(String macAddress,int opt) {
		Log.i(THIS_FILE, "onReqConnectWatch  macAddress=" +macAddress+",opt ="+opt);
		if(mListener!= null){
			mListener.connectWatch(macAddress,opt);
		}
	}

	@Override
	public void onIgstStatusAck() {

	}

	public void onVol(int keyCode)
	{
		CallOperator op = product.getSuperCallOperator().getCallOperator();
		switch (keyCode) {
			case KeyEvent.KEYCODE_VOLUME_UP:
				op.onVol(AppRTCAudioManager.Vol_Type_Up);
				break;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				op.onVol(AppRTCAudioManager.Vol_Type_Down);
				break;
			case KeyEvent.KEYCODE_VOLUME_MUTE:

				break;
		}
//		ISuperCallOperator op = product.getSuperCallOperator();
//		switch (keyCode) {
//			case KeyEvent.KEYCODE_VOLUME_UP:
//				op.doUserAction(curCallKey, CallConst.BtnAction_Vol_Up);
//				break;
//			case KeyEvent.KEYCODE_VOLUME_DOWN:
//				op.doUserAction(curCallKey, CallConst.BtnAction_Vol_Down);
//				break;
//			case KeyEvent.KEYCODE_VOLUME_MUTE:
//
//				break;
//		}
	}

	@Override
	public void onCloseApplication(int isopen) {
		Log.i(THIS_FILE, "onCloseApplication isopen="+isopen);
		if (mListener != null) {
			mListener.closeApplication(isopen);
		}
	}

	@Override
	public void onReqConnectWatchStatus() {
		//��ȡ��ǰ�������mac��name
		Log.i(THIS_FILE, "getConnectWatchInfo");
		if (mListener != null) {
			mListener.getConnectWatchInfo();
		}
	}

	//	@Override
//	public void onDisplayVideo(int callId, boolean show) {
//		// TODO Auto-generated method stub
//
//	}
	@Override
	public void updateMediaStatus(String callKey, int mediaStatus)
	{}
	public void setGroup(boolean isGroup) {
		Log.d(THIS_FILE,"pttBtnAction setGroup = " + isGroup);
		this.isGroup = isGroup;
	}

	public String getLastStateDesp() {
		return lastStateDesp;
	}

	private RollbackTimer rollbackTimer = null;
	private int time = 4;

	public void startRtcpKeepAlive()
	{
		Log.d(THIS_FILE, "startRtcpKeepAlive");
		this.time = 4;
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		rollbackTimer = new RollbackTimer(ctx,2,this);
		callOperator.negotiateRtcpChannel();
	}
	public void rollback()
	{
		Log.d(THIS_FILE, "rollback");
		if(time > 0) {
			time --;
			sendAndRestartRtcpKeepAlive();
		}
		else
		{
			rollbackTimer = null;
		}
	}
	private void sendAndRestartRtcpKeepAlive()
	{
		if(rollbackTimer != null) {
			rollbackTimer.restartTimer(2);
		}
		callOperator.negotiateRtcpChannel();
	}

	public void setRecvSos(boolean recvSos) {
		isRecvSos = recvSos;
	}

	public boolean isRecvSos() {
		return isRecvSos;
	}

	public String getLastFloorStr() {
		return lastFloorStr;
	}

	public void setUpdateUiListener(UpdateUiListener updateListener) {
		this.updateUiListener = updateListener;
	}

	@Override
	public void afterSetDefaultGroup() {
		Log.d(THIS_FILE, "afterSetDefaultGroup");
		lastStateDesp = callDescInfo.getErrDesc(CallConst.FloorState_NoPermission);
		lastFloorStr = callDescInfo.getErrDesc(CallConst.FloorState_NoPermission);
	}

	public void setSingleHalfCallNotify(ISingleHalfCallNotify singleHalfCallNotify) {
		this.singleHalfCallNotify = singleHalfCallNotify;
	}
}
